package cn.neusoft.luofa603.yuejian_29;

import cn.neusoft.luofa603.yuejian_29.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/18.
 */

public class Model {

    private static final Model ourInstance = new Model();
    public static UserService userService;


    private Model() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        userService = retrofit.create(UserService.class);

    }

    public static <T> Builder<T> with(Call<T> call){
        return new Builder<>(call);
    }

    public static class Builder <B>{
        private Call<B> call;

        private Positive positive = null;
        private Negative negative = null;
        private Failure failure = null;
        private Before before = null;
        private After after = null;

        Builder(Call<B> call){
            this.call = call;
        }

        public Builder positive(Positive<?> success){
            this.positive = success;
            return this;
        }

        public Builder negative(Negative negative){
            this.negative = negative;
            return this;
        }

        public Builder failure(Failure failure){
            this.failure = failure;
            return this;
        }

        public Builder before(Before before){
            this.before = before;
            return this;
        }

        public Builder after(After after){
            this.after = after;
            return this;
        }

        public void request() {
            if (positive == null)
                positive = new Positive() {
                    @Override
                    public void onPositive(Object data) {

                    }
                };
            if (negative == null)
                negative = new Negative() {
                    @Override
                    public void onNegative() {

                    }
                };
            if (failure == null)
                failure = new Failure() {
                    @Override
                    public void onFailure() {

                    }
                };
            if (before == null)
                before = new Before() {
                    @Override
                    public void before() {

                    }
                };
            if (after == null)
                after = new After() {
                    @Override
                    public void after() {

                    }
                };

            call.enqueue(new Callback<B>() {
                @Override
                public void onResponse(Call<B> call, Response<B> response) {
                    if (response.body()!=null) {
                        before.before();
                        positive.onPositive(response.body());
                        after.after();
                    } else {
                        negative.onNegative();
                    }
                }

                @Override
                public void onFailure(Call<B> call, Throwable t) {
                    failure.onFailure();
                }
            });

        }

    }

    public interface Positive<T>{
        void onPositive(T data);
    }

    public interface Negative {
        void onNegative();
    }

    public interface Failure {
        void onFailure();
    }

    public interface Before{
        void before();
    }

    public interface After{
        void after();
    }

}
